﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DusonFramework.Caching.DevFx.Config;
using DusonFramework.Config;

namespace DusonFramework.Caching.DevFx
{
    public class DevFxCache : DusonFramework.Caching.ICache
    {
        private ICache _cache;

        public DevFxCache(string cacheName)
        {
            if (SectionHandler.Current == null)
                throw new CacheException("缓存没有配置");

            string xml = SectionHandler.Current.OuterXml;
            IConfigSetting setting = ConfigHelper.CreateFromXmlString(xml);

            ICacheManager cacheManager = setting.Property["type"].ToObject<ICacheManager>();
            cacheManager.Init(setting);

            _cache = cacheManager.GetCache(cacheName);
        }

        public void Add(string key, object value)
        {
            _cache.Add(key, value);
        }

        public void AddWithFilesDepend(string key, object value, string[] files)
        {
            if (files.Length != 1)
                throw new ArgumentException("files", "只能依赖一个文件");

            ICacheDependency depend = new FileCacheDependency(files[0]);
            _cache.Add(key, value, depend);
        }

        public void AddWithCacheItemsDepend(string key, object value, string[] dependKey)
        {
            throw new NotImplementedException();
        }

        public void Remove(string key)
        {
            _cache.Remove(key);
        }

        public bool Contains(string key)
        {
            return _cache.Contains(key);
        }

        public object Get(string key)
        {
            return _cache.Get(key);
        }

        /// <summary>
        /// 设置到期相对时间[单位: 秒] 
        /// </summary>
        public virtual int TimeOut
        {
            set { throw new NotImplementedException(); }
            get { throw new NotImplementedException(); }
        }
    }
}
